//
//  HSListDetailVM.m
//  HolyShit
//
//  Created by Robin on 2017/3/24.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSListDetailVM.h"
#import "NSString+Html.h"

@interface HSListDetailVM ()

@property (nonatomic, copy) NSString *postId;

@end

@implementation HSListDetailVM

- (instancetype)initWithPostId:(NSString *)postId
{
    if (self = [super init]) {
        _postId = postId;
    }
    return self;
}

- (RACCommand *)dataCommand
{
    if (!_dataCommand) {
        @weakify(self);
        _dataCommand = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            NSString *url = [NSString stringWithFormat:@"c.m.163.com/nc/article/%@/full.html",self.postId];
            return [[MYHttpHelper shareHelper] rac_getWithUrl:url params:nil success:^(NSDictionary *dict) {
                @strongify(self);
                self.detailModel = [HSListDetailModel modelWithDictionary:dict[self.postId]];
                [self updateData];
            } failed:^(NSError *error) {
                
            }].racSignal;
        }];
    }
    return _dataCommand;
}

- (void)updateData
{
    for (HSListImgModel *imgModel in self.detailModel.img) {
        NSString *str = [NSString stringWithFormat:@"<img src='%@' >",imgModel.src];
        self.detailModel.body = [self.detailModel.body replaceRegStr:imgModel.ref WithStr:str];
    }
    for (HSListLinkModel *linkModel in self.detailModel.link) {
        self.detailModel.body = [self.detailModel.body replaceRegStr:linkModel.ref WithStr:@""];
    }
    
    NSArray *array = @[@"b", @"/b" , @"ins", @"/ins", @"font", @"/font", @"a", @"/a"];
    for (NSString *str in array) {
        self.detailModel.body = [self.detailModel.body replaceRegStr:[NSString stringWithFormat:@"<%@[^<>]*>", str] WithStr:@""];
    }
    self.detailModel.body = [self.detailModel.body replaceRegStr:@"网易.*?报道.{1}" WithStr:@""];
}

@end
